﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using xiaodingmarket_application.Interfaces;
using xiaodingmarket_application.ViewModel;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Domain_core.Notificatiions;

namespace xiaodingmarket_service.Controllers
{
    //[Authorize]  //实现验证
    public class CommodityController : ApiController
    {
        private readonly ICommodityService _commodityService;

        public CommodityController(ICommodityService commodityService,INotificationHandler<DomainNotification> notifications, IMediatorHandler mediator) : base(notifications, mediator)
        {
            _commodityService = commodityService;
        }
        /// <summary>
        /// 获取kind(1商品2悬赏)所有商品或悬赏信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]  //可以让所有人访问
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("Commodity/kind")]
        public IActionResult Get(int kind)
        {
            var commodityViewModels = _commodityService.GetAll(kind);
            List<CommodityViewModel> Commdity = new List<CommodityViewModel>();
            foreach (var commodityViewModel in commodityViewModels)
            {
                //如果截止的时间早于现在，说明已过期
                if (DateTime.Compare(Convert.ToDateTime(commodityViewModel.endtime), DateTime.Now) < 0)
                {
                    _commodityService.Remove(commodityViewModel.CommodityId);
                }
                else
                {
                    Commdity.Add(commodityViewModel);
                }
            }
            return Response(Commdity);
            
        }
        /// <summary>
        /// 根据商品id获取详情,userid后端判断是不是用户发布人，不是浏览+1
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("Commodity/{id:guid}")]
        public IActionResult Get(Guid id,Guid userid)
        {
            var commodityViewModel = _commodityService.GetById(id,userid);

            if (DateTime.Compare(Convert.ToDateTime(commodityViewModel.endtime), DateTime.Now) < 0)
            {
                _commodityService.Remove(commodityViewModel.CommodityId);
                return Response(commodityViewModel);
            }
            else
            {
                return Response(commodityViewModel);
            }
           
        }
        /// <summary>
        /// 通过标签查询，可用于商品和悬赏搜索
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("Commodity/remark")]
        public IActionResult Get(string remark)
        {
            var commodityViewModels = _commodityService.GetAnyByRemark(remark);
            List<CommodityViewModel> Commdity = new List<CommodityViewModel>();
            foreach(var commodityViewModel in commodityViewModels)
            {
                //如果截止的时间早于现在，说明已过期
                if (DateTime.Compare(Convert.ToDateTime(commodityViewModel.endtime), DateTime.Now) < 0)
                {
                    _commodityService.Remove(commodityViewModel.CommodityId);
                }
                else
                {
                    Commdity.Add(commodityViewModel);
                }
            }
            return Response(Commdity);
        }
        /// <summary>
        /// 根据小类进行商品的查询
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("Commodity/category")]
        public IActionResult GetByCategory(int category)
        {
            var commodityViewModels = _commodityService.GetAnyByCategory(category);
            List<CommodityViewModel> Commdity = new List<CommodityViewModel>();
            foreach (var commodityViewModel in commodityViewModels)
            {
                //如果截止的时间早于现在，说明已过期
                if (DateTime.Compare(Convert.ToDateTime(commodityViewModel.endtime), DateTime.Now) < 0)
                {
                    _commodityService.Remove(commodityViewModel.CommodityId);
                }
                else
                {
                    Commdity.Add(commodityViewModel);
                }
            }
            return Response(Commdity);
        }
        /// <summary>
        /// 发布商品或悬赏接口
        /// </summary>
        /// <param name="commodityViewModel"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("Commodity")]
        public IActionResult Post([FromBody]CommodityViewModel commodityViewModel)
        {
            if (!ModelState.IsValid)
            {
                NotifyModelStateErrors();
                return Response(commodityViewModel);
            }
            _commodityService.Create(commodityViewModel);

            return Response(commodityViewModel);
        }
        /// <summary>
        /// 修改商品或悬赏
        /// </summary>
        /// <param name="commodityViewModel"></param>
        /// <returns></returns>
        [HttpPut]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("Commodity")]
        public IActionResult Put([FromBody]CommodityViewModel commodityViewModel)
        {
            if (!ModelState.IsValid)
            {
                NotifyModelStateErrors();
                return Response(commodityViewModel);
            }
            _commodityService.Update(commodityViewModel);

            return Response(commodityViewModel);
        }
        
        /// <summary>
        /// 删除单个商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize(Policy = "CanBasicoperation")]
        [Route("Commodity")]
        public IActionResult Delete(Guid id)
        {
            _commodityService.Remove(id);
            return Response();
        }
        
        /// <summary>
        /// 删除多个商品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize(Policy = "CanBasicoperation")]
        [Route("Commodities")]
        public IActionResult Delete(Guid[] ids)
        {
            _commodityService.RemoveAll(ids);

            return Response();
        }
    }
}
